package com.cipe.cmrs.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.cipe.cmrs.model.Donor;
import com.cipe.cmrs.model.Donoraward;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.service.CountryService;
import com.cipe.cmrs.service.DonorService;
import com.cipe.cmrs.service.DonorawardstatusService;
import com.cipe.cmrs.service.DonorawardtypeService;
import com.cipe.cmrs.service.StateService;
import com.cipe.cmrs.service.UserService;
import com.cipe.cmrs.util.DateUtil;
import com.cipe.cmrs.validator.DonorValidator;

@Controller
public class DonorController extends BaseController {

	private static final long serialVersionUID = 1815579886981166259L;
	private static final String PARAM_PREV_URL = "prevUrl";
	private String formView = "editDonor";
	
	@Autowired
	private DonorService donorService;
	@Autowired
	private CountryService countryService;
	@Autowired
	private StateService stateService;
	@Autowired
	private DonorawardtypeService donorawardtypeService;
	@Autowired
	private DonorawardstatusService donorawardstatusService;
	@Autowired
	private UserService userService;
	@Autowired
	private DonorValidator donorValidator;

	@RequestMapping("/secure/showAddDonor.do") 
	public String showAddDonor(Model model, HttpServletRequest request) {
		Donor donor = new Donor();
		User user = userService.findById(getUserId(request));
		donor.setCreatedBy(user);
		donor.setCreationDate(DateUtil.getCurrentDate());
		donor.setUpdatedBy(user);
		donor.setUpdationDate(DateUtil.getCurrentDate());
		model.addAttribute("donor", donor);
		model.addAttribute("countryList", countryService.getAllCountries());
		
		// TODO: this should be refactored here to fetch the first country's states
		//model.addAttribute("stateList", stateService.findAll());  
		return formView;
	}
	
	@RequestMapping("/secure/showEditDonor.do")
	public String showEditDonor(@RequestParam Integer id, Model model, HttpServletRequest request) {
		Donor donor  = donorService.findById(id);
		model.addAttribute("donor", donor);
		model.addAttribute("countryList", countryService.getAllCountries());
		model.addAttribute("stateList", stateService.findAll());
		model.addAttribute(PARAM_PREV_URL, request.getParameter(PARAM_PREV_URL));
		return formView;
	}

	@RequestMapping("/secure/saveDonor.do")
	public String SaveDonor(@ModelAttribute("donor") Donor donor, BindingResult result, Model model, HttpServletRequest request) {
		model.addAttribute("donorList", donorService.findAll());
		model.addAttribute("countryList", countryService.getAllCountries());
		model.addAttribute("donorawardtypeList", donorawardtypeService.findAll());
		model.addAttribute("donorawardstatusList", donorawardstatusService.findAll());

		String saveAction = getText("action.save");
		String action = request.getParameter("action");
		donorValidator.validate(result, donor);
		if (result.hasErrors()) {
			model.addAllAttributes(result.getModel());
			saveError(request, result.getGlobalErrors());
		} else {
			try {
				donor = donorService.saveDonor(donor, getUserId(request));
				if (saveAction.equals(action.toLowerCase())) {
					saveMessage(request, getText("editDonor.message.success"));
				}
			} catch (Exception e) {
				logger.error(getText("error.transaction"), e);
				saveError(request, getText("error.transaction"));
			}
		}

		if (hasError(request) || saveAction.equals(action.toLowerCase())) {
			model.addAttribute("donor", donor);
			return formView;
		} else {
			Donoraward donoraward = new Donoraward();
			User user = userService.findById(getUserId(request));
			donoraward.setCreatedBy(user);
			donoraward.setCreationDate(DateUtil.getCurrentDate());
			donoraward.setUpdatedBy(user);
			donoraward.setUpdationDate(DateUtil.getCurrentDate());		
			model.addAttribute("donor", donor);
			donoraward.setDonor(donor);
			model.addAttribute("donoraward", donoraward);
			return "editDonoraward";
		}
	}

	@RequestMapping("/secure/showDonorList.do")
	public String showDonorList(Model model) {
		model.addAttribute("donors", donorService.getAllDonors());
		return "donorList";
	}
}
